package com.cyf.plc.ui

import android.os.Bundle
import com.cyf.library.util.UIUtils
import com.cyf.plc.base.BaseActivity
import com.cyf.plc.constant.Constants.Companion.ATTR_AIR_VOLUME
import com.cyf.plc.constant.Constants.Companion.ATTR_CARBON_DIOXIDE
import com.cyf.plc.constant.Constants.Companion.ATTR_FAULT
import com.cyf.plc.constant.Constants.Companion.ATTR_HUMIDITY
import com.cyf.plc.constant.Constants.Companion.ATTR_LEAVE_HOME
import com.cyf.plc.constant.Constants.Companion.ATTR_MODE
import com.cyf.plc.constant.Constants.Companion.ATTR_PM25
import com.cyf.plc.constant.Constants.Companion.ATTR_POWER
import com.cyf.plc.constant.Constants.Companion.ATTR_SET_TEMP
import com.cyf.plc.constant.Constants.Companion.ATTR_TEMP
import com.cyf.plc.constant.Constants.Companion.ATTR_VOC
import com.cyf.plc.constant.Constants.Companion.PANEL_ATTR_IDS
import com.cyf.plc.constant.Constants.Companion.PLC_SUB_ATTRIBUTE_IDS
import com.cyf.plc.constant.Constants.Companion.SYSTEM_ATTR_IDS
import com.cyf.plc.constant.Constants.Companion.UNDERLINE
import com.cyf.plc.constant.Constants.Companion.VALUE_0
import com.cyf.plc.constant.Constants.Companion.VB
import com.cyf.plc.constant.Constants.Companion.VW
import com.cyf.plc.databinding.ActivityMainBinding
import com.cyf.plc.model.*
import com.cyf.plc.plc.PlcExecutor
import com.cyf.plc.util.viewModelOf
import com.cyf.plc.viewmodel.MainViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi

@ExperimentalCoroutinesApi
class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {

    private val plcAttributes = ArrayList<PlcAttribute>()

    private val plcSubAttributes = ArrayList<PlcSubAttribute>()

    private val plcFloors = ArrayList<PlcFloor>()

    private val plcRooms = ArrayList<PlcRoom>()

    private val plcFaults = ArrayList<PlcFault>()

    override fun onDestroy() {
        super.onDestroy()
        PlcExecutor.closePlc()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //插入默认配置数据
        insertDefaultConfigs()
    }

    private fun insertDefaultConfigs() {
        insertPlcFaults()

        //测试插入
        //testInsert()

        //系统状态
        addPlcAttribute(SYSTEM_ATTR_IDS[8], 100, ATTR_AIR_VOLUME, "风量")
        addPlcAttribute(SYSTEM_ATTR_IDS[0], 102, ATTR_MODE, "模式")
        addPlcAttribute(SYSTEM_ATTR_IDS[1], 104, ATTR_POWER, "开关")
        addPlcAttribute(SYSTEM_ATTR_IDS[7], 106, ATTR_LEAVE_HOME, "离家")
        addPlcAttribute(SYSTEM_ATTR_IDS[9], 128, ATTR_FAULT, "故障")

        //系统传感器
        addPlcAttribute(SYSTEM_ATTR_IDS[2], 620, ATTR_TEMP, "温度")
        addPlcAttribute(SYSTEM_ATTR_IDS[3], 622, ATTR_HUMIDITY, "湿度")
        addPlcAttribute(SYSTEM_ATTR_IDS[5], 624, ATTR_PM25, "pm25")
        addPlcAttribute(SYSTEM_ATTR_IDS[6], 626, ATTR_VOC, "voc")
        addPlcAttribute(SYSTEM_ATTR_IDS[4], 628, ATTR_CARBON_DIOXIDE, "二氧化碳")


        //配置控制属性
        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[0], 0f, "制冷"))
        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[1], 1f, "制热"))
        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[2], 2f, "通风"))
        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[3], 3f, "除湿"))

        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[4], 1f, "开关开"))
        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[5], 0f, "开关关"))

        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[6], 1f, "离家开"))
        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[7], 0f, "离家关"))

        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[8], 1f, "面板开"))
        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[9], 0f, "面板关"))

        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[10], 1f, "风量2"))
        plcSubAttributes.add(PlcSubAttribute(PLC_SUB_ATTRIBUTE_IDS[11], 0f, "风量3"))

        //测试保存
        mViewModel.savePlcAttributes(plcAttributes)
        mViewModel.savePlcSubAttributes(plcSubAttributes)
    }

    private fun testInsert() {
        plcFloors.add(PlcFloor(1, "一楼"))

        plcRooms.add(PlcRoom(1, "主卧", 1))
        plcRooms.add(PlcRoom(2, "次卧", 1))

        mViewModel.saveFloors(plcFloors)
        mViewModel.saveRooms(plcRooms)

        //添加2个测试面板
        addPlcAttribute("1_" + PANEL_ATTR_IDS[2], 146, ATTR_POWER, "开关")
        addPlcAttribute("1_" + PANEL_ATTR_IDS[1], 132, ATTR_TEMP, "温度")
        addPlcAttribute("1_" + PANEL_ATTR_IDS[2], 144, ATTR_SET_TEMP, "设定温度")
        addPlcAttribute("1_" + PANEL_ATTR_IDS[3], 136, ATTR_HUMIDITY, "湿度")
    }

    private fun addPlcAttribute(
        attributeId: String,
        offSet: Int,
        attributeName: String,
        name: String
    ) {
        plcAttributes.add(
            PlcAttribute(
                attributeId,
                offSet,
                VW,
                attributeName,
                name
            )
        )
    }

    private fun insertPlcFaults() {
        for (i in 1..30) {
            plcFaults.add(PlcFault(i, 130 + 20*i, VB, "$i#面板故障", VALUE_0, 0))
        }

        plcFaults.add(PlcFault(31, 126, VB, "vb126故障", VALUE_0, 0))
        plcFaults.add(PlcFault(32, 127, VB, "vb127故障", VALUE_0, 0))
        plcFaults.add(PlcFault(33, 128, VB, "vb128故障", VALUE_0, 0))
        plcFaults.add(PlcFault(34, 129, VB, "vb129故障", VALUE_0, 0))

        mViewModel.savePlcFaults(plcFaults)
    }

    override fun onResume() {
        super.onResume()
        UIUtils.hideNavigation(this)
    }

    override fun getViewBinding(): ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)

    override fun createViewModel(): MainViewModel = viewModelOf(mViewModelProvider)

    override fun initView() {

    }
}